package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


@RestController
@Api("菜品管理")
@Slf4j
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Qualifier("redisTemplate")//明确指明注入的是哪一个Bean
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDishwithFlavor(@RequestBody DishDTO dishDTO){ //好像确实是只有传进来是json数据才用这个注解
        log.info("新增菜品{}",dishDTO);
        dishService.addDishwithFlavor(dishDTO);//操作菜单表和口味表两张表
        //根据新增菜品的分类id清理缓存
        String key="dish"+dishDTO.getCategoryId();
        cleanRedisCache(key);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> dishPage(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询：{}", dishPageQueryDTO);
        PageResult page=dishService.dishPage(dishPageQueryDTO);
        log.info("返回页面数据是：{}",page);
        return Result.success(page);
    }

    @DeleteMapping
    @ApiOperation("删除菜品,批量删除")
    public Result deleteDishwihFlavor(@RequestParam List<Long> ids){//请求参数类型是集合参数，请求参数名应该和集合名一样都是ids，所以@RequestParam绑定关系
        log.info("删除菜品：{}",ids);
        dishService.deleteDishwithFlavor(ids);
        //直接清除所有key,清除的key的形式是由前面添加key的逻辑决定的！
        cleanRedisCache("dish_*");
        return Result.success();
    }

    //修改菜品=查询回显+修改
    @GetMapping("/{id}")
    @ApiOperation("根据id查询单个菜品及其口味")
    public Result<DishVO> getDishwithFlavors(@PathVariable Long id){
        log.info("根据id：{}查询菜品",id);
        DishVO dishVO=dishService.getDishwithFlavors(id);
        return Result.success(dishVO);
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result updateDishwithFlavors(@RequestBody DishDTO dishDTO){  //因为包含菜品+口味
        log.info("根据id修改对应菜品和口味，{}",dishDTO);
        dishService.updateDishwithFlavors(dishDTO);
        cleanRedisCache("dish_*");
        return Result.success();
    }
//以下是导入套餐相关功能
    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    //todo 需要完善菜品的起售停售功能，并加入清理缓存逻辑

    //清理缓存数据
    public void cleanRedisCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
